package ua.org.cityid.client.controls;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class RegionAnchor extends Composite {
	private static Anchor selectedAnchor = null;

	private static RegionAnchorUiBinder uiBinder = GWT
			.create(RegionAnchorUiBinder.class);

	interface RegionAnchorUiBinder extends UiBinder<Widget, RegionAnchor> {
	}

	interface SelectionStyle extends CssResource {
		String selectedRegion();
	}

	@UiField
	Anchor region;
	@UiField
	SelectionStyle selectionStyle;
	
	@UiHandler("region")
	void onClick(ClickEvent e) {
		switchSelectedRegion();
	}

	public void switchSelectedRegion() {
		String style = selectionStyle.selectedRegion();
		if (selectedAnchor != null)
			selectedAnchor.removeStyleName(style);
		selectedAnchor = region;
		selectedAnchor.addStyleName(style);
	}

	private int regionId = 0;

	@Inject
	public RegionAnchor(int regionId, String caption) {
		initWidget(uiBinder.createAndBindUi(this));
		setRegionId(regionId);
		setCaption(caption);
	}

	private void setCaption(String caption) {
		region.setText(caption);
	}
	

	public int getRegionId() {
		return regionId;
	}

	private void setRegionId(int regionId) {
		this.regionId = regionId;
	}
	
	public HasClickHandlers getRegion(){
		return region;
	}
}
